// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   MACUtil.java

package com.iso8583.util;


// Referenced classes of package com.solab.iso8583.util:
//            HexCodec

public class MACUtil
{

    public MACUtil()
    {
    }

    public static byte[] bcd2Ascii(byte bytes[])
    {
        byte temp[] = new byte[bytes.length * 2];
        for(int i = 0; i < bytes.length; i++)
        {
            temp[i * 2] = (byte)(bytes[i] >> 4 & 0xf);
            temp[i * 2 + 1] = (byte)(bytes[i] & 0xf);
        }

        return temp;
    }

    private String fullZero(String para, int filLen)
    {
        StringBuffer sbff = new StringBuffer();
        for(int i = 0; i < filLen; i++)
            sbff.append("0");

        return (new StringBuilder(String.valueOf(para))).append(sbff.toString()).toString();
    }

    public String getMacCalData(String data)
    {
        int dataLen = data.length() / 2;
        byte buf[] = new byte[8];
        int len = Integer.valueOf(dataLen).intValue();
        int l = len / 8 + 1;
        int filLen = l * 16 - data.length();
        String tmpData = fullZero(data, filLen);
        byte inbuf[] = HexCodec.hexDecode(tmpData);
        for(int i = 0; i < l; i++)
        {
            for(int k = 0; k < 8; k++)
                buf[k] = (byte)(buf[k] ^ inbuf[i * 8 + k]);

        }

        String makCal = ascii2Str(bcd2Ascii(buf));
        return makCal;
    }

    private byte[] getMACData(byte d8583[])
    {
        int len = d8583.length - 8;
        byte ret[] = new byte[len];
        System.arraycopy(d8583, 0, ret, 0, len);
        return ret;
    }

    public static String ascii2Str(byte ascii[])
    {
        StringBuffer res = new StringBuffer();
        for(int i = 0; i < ascii.length; i++)
            res.append(strValue(ascii[i]));

        return res.toString();
    }

    private static char strValue(byte asc)
    {
        return ascii[asc];
    }

    static final char ascii[] = "0123456789ABCDEF".toCharArray();

}
